<?php
/**
 * Created by PhpStorm.
 * User: 小灰灰
 * Date: 2022-06-17
 * Time: 11:43:38
 * Info:
 */

namespace app\admin\controller\system;

use app\admin\annotation\ControllerAnnotation;
use app\admin\annotation\NodeAnotation;
use app\admin\controller\CommonController;
use app\admin\service\NodeService;
use support\Request;
use think\facade\Db;
use app\common\model\AuthRule as AuthRuleModel;

/**
 * @ControllerAnnotation(title="系统节点管理")
 * Class Node
 * @package app\admin\controller\system
 */
class NodeController extends CommonController
{

    /**
     * @NodeAnotation(title="节点列表")
     */
    public function index(Request $request)
    {
        if ($request->isAjax()) {
            $AuthRuleModel = new AuthRuleModel();
            $count         = $AuthRuleModel->count();
            $list          = $AuthRuleModel->getNodeTreeList();

            return $this->success('ok', $list, $count);
        }

        return view('node/index');
    }

    /**
     * @NodeAnotation(title="系统节点更新")
     */
    public function refresh_node(Request $request)
    {
        try {
            $nodeList = (new NodeService())->getNodelist();
        } catch (\Exception $e) {
            return error_json("节点更新失败，查看是否有控制器没有引用：Annotations或者注释写错了<br>".$e->getMessage());
        }
        if (empty($nodeList)) {
            return error_json('暂无需要更新的系统节点');
        }
        $force = $request->input('force');

        $AuthRuleModel = new AuthRuleModel();
        if ($force == 1) {
            $updateNodeList = $AuthRuleModel->whereIn('node', array_column($nodeList, 'node'))->select();
            $formatNodeList = array_format_key($nodeList, 'node');

            foreach ($updateNodeList as $vo) {
                isset($formatNodeList[$vo['node']]) && $AuthRuleModel->where('id', $vo['id'])->update([
                    'title'   => $formatNodeList[$vo['node']]['title'],
                    'is_auth' => $formatNodeList[$vo['node']]['is_auth'],
                ]);
            }
        }

        $existNodeList = $AuthRuleModel->field('node,title,type,is_auth')->select();
        foreach ($nodeList as $key => $vo) {
            foreach ($existNodeList as $v) {
                if ($vo['node'] == $v->node) {
                    unset($nodeList[$key]);
                    break;
                }
            }
        }
        $AuthRuleModel->saveAll($nodeList);

        return $this->success('节点更新成功');
    }

    /**
     * @NodeAnotation(title="清除失效节点")
     */
    public function clear_node()
    {
        $nodeList      = (new NodeService())->getNodelist();
        $AuthRuleModel = new AuthRuleModel();
        try {
            $existNodeList  = $AuthRuleModel->field('id,node,title,type,is_auth')->select()->toArray();
            $formatNodeList = array_format_key($nodeList, 'node');
            foreach ($existNodeList as $vo) {
                ! isset($formatNodeList[$vo['node']]) && $AuthRuleModel->where('id', $vo['id'])->delete();
            }
        } catch (\Exception $e) {
            return error_json('节点更新失败');
        }

        return $this->success('节点更新成功');
    }

}
